package com.smarthome.web.property.controller.deviceManager;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.deviceManager.EquipmentUserService;
import com.smarthome.domain.bean.ShEquipmentUser;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 设备与用户对应关系管理
 * @author szc
 *
 */
@Controller
@RequestMapping("/equipmentUserController")
public class EquipmentUserController {
	
	@Resource
    private EquipmentUserService equipmentUserService;
	
	/**
	 * 获取信息列表--小区绑定类型
	 * @param accountLog
	 * @param page
	 * @return
	 */
	@RequestMapping("/dataGridForXq")
    @ResponseBody
    public DataGrid dataGridForXq(ShEquipmentUser equipmentUser, PageHelper page) {
		equipmentUser.setBingType("2");////绑定类型：1：设备与用户关联绑定；2：设与小区绑定
       
		//得到当前登录人信息
    	if(UserHolder.isSuperAdmin()){//超级管理员,查询所有

    	}else{  //其他角色这查询自己小区的设备
    		Long residenceId = UserHolder.getCurrentResidenceId();//小区所属id
    		if(residenceId!=null){
    			equipmentUser.setResidenceId(residenceId);
    		}else{
    			equipmentUser.setResidenceId(-1L);//无法获取小区id，就查询不到数据
    		}
    	}
		return equipmentUserService.dataGrid(equipmentUser, page);
    }

	/**
	 * 用户绑定查询
	 * @param equipmentUser
	 * @param page
	 * @return
	 */
	@RequestMapping("/dataGridForUser")
    @ResponseBody
    public DataGrid dataGridForUser(ShEquipmentUser equipmentUser, PageHelper page) {
		equipmentUser.setBingType("1");//绑定类型：1：设备与用户关联绑定；2：设与小区绑定
		//得到当前登录人信息
    	if(UserHolder.isSuperAdmin()){//超级管理员,查询所有

    	}else{  //其他角色这查询自己小区的设备
    		Long residenceId = UserHolder.getCurrentResidenceId();//小区所属id
    		if(residenceId!=null){
    			equipmentUser.setResidenceId(residenceId);
    		}else{
    			equipmentUser.setResidenceId(-1L);//无法获取小区id，就查询不到数据
    		}
    	}
        return equipmentUserService.dataGrid(equipmentUser, page);
    }
	
	
	/**
	 * 新增--小区绑定页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/addResidencePage")
    public String addResidencePage(HttpServletRequest request,HttpServletResponse response) {
        return "/deviceManager/deviceResidenceAdd";
    }
	
	
	/**
     * 新增--设备与用户绑定页面
     * 
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(HttpServletRequest request,HttpServletResponse response) {
    	//String type =request.getParameter("type");//绑定类型：1：设备与用户关联绑定；2：设与小区绑定
    	//request.setAttribute("type", type);
        return "/deviceManager/deviceUserAdd";
    }
	
    /**
     * 新增--保存信息
     * @param deviceSupplier
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Result add(ShEquipmentUser equipmentUser,HttpServletRequest request) {
    	DateFormat objMyformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
    	try {
    		String bingType = request.getParameter("bingType");//绑定类型
    		String srcResidenceId = request.getParameter("srcResidenceId");//小区id
        	if(org.apache.commons.lang.StringUtils.isNotBlank(srcResidenceId)){
        		equipmentUser.setResidenceId( Long.valueOf(srcResidenceId));
        	}else{
        		return Result.failure("请选择【小区】选项！");
        	}
    		String srcEquipmentInfoId = request.getParameter("equipmentInfoId");//设备基础信息id
        	if(org.apache.commons.lang.StringUtils.isNotBlank(srcEquipmentInfoId)){
        		equipmentUser.setEquipmentInfoId( Long.valueOf(srcEquipmentInfoId));
        	}else{
        		return Result.failure("请选择【绑定设备】选项！");
        	}
        	if(StringUtils.isBlank(equipmentUser.getAddress())){//设备位置
        		return Result.failure("请选择【设备位置】选项！");
        	} else{
        		
        	}
        	if(equipmentUser.getCommType()==null){//通信类型
        		return Result.failure("请选择【通信类型】选项！");
        	}
        	if(equipmentUser.getStatus()==null){//通信状态
        		return Result.failure("请选择【通信状态】选项！");
        	}
        	
        	String doorId = request.getParameter("srcDoorId");//门牌id
        	if(org.apache.commons.lang.StringUtils.isNotBlank(doorId)){
        		equipmentUser.setDoorId( Long.valueOf(doorId));
        	}
        	equipmentUser.setGmtCreate(new Date());
        	//equipmentUser.setLastCommTime(new Date());//最后通信时间
        	equipmentUser.setBingType(bingType);//绑定类型：1：设备与用户关联绑定；2：设与小区绑定
        	equipmentUserService.add(equipmentUser);
            return Result.success("添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failure("添加信息失败！");
		}
    }
    
    
    /**
     * 编辑--设备与用户绑定页面
     * @param req
     * @param id
     * @return
     */
    @RequestMapping("/editUserPage")
	public String editUserPage(HttpServletRequest req,Long id){
    	ShEquipmentUser deviceUser=equipmentUserService.queryShEquipmentUserById(id);
		req.setAttribute("deviceUser", deviceUser);
		return "/deviceManager/deviceUserEdit";
	}
    
    /**
     * 编辑--设备与小区绑定页面
     * @param req
     * @param id
     * @return
     */
    @RequestMapping("/editResidencePage")
	public String editResidencePage(HttpServletRequest req,Long id){
    	ShEquipmentUser deviceUser=equipmentUserService.queryShEquipmentUserById(id);
		req.setAttribute("deviceUser", deviceUser);
		return "/deviceManager/deviceResidenceEdit";
	}
    
    
    /***
     * 修改--更新操作
     * @param deviceSupplier
     * @return
     */
    @RequestMapping("/edit")
	@ResponseBody
	public Result edit(ShEquipmentUser equipmentUser,HttpServletRequest request) {
    	try {
    		equipmentUserService.edit(equipmentUser);
    		return Result.success("更改成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failure("修改信息失败！");
		}
    	
	}
    
    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
	@ResponseBody
	public Result delete(Long id){
    	equipmentUserService.delete(id);
		return Result.success("删除成功！");
	}
}
